package com.cedarhd.brzx.domain.service;

import com.cedarhd.brzx.common.enums.OrderMethodEnum;
import com.cedarhd.brzx.dto.request.RechargeDTO;
import org.json.JSONObject;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2018/5/2.
 */
@Service
public class SoaOrderService extends SoaBaseService{

    private static Logger log = LoggerFactory.getLogger(SoaMemberService.class);

    private static String soaName = "OrderService";

    private String ordErexpireDatetime = null;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Before
    public void before(){
        //订单过期时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, 15);
        Date date = calendar.getTime();
        ordErexpireDatetime = sdf.format(date);
    }

    /**
     * 充值申请
     * @param rechargeDTO
     * @return
     */
    public JSONObject depositApply (RechargeDTO rechargeDTO){
        if (rechargeDTO == null) {
            return null;
        }
        try{
            //支付方式
            //快捷
            //JSONObject quickPay = new JSONObject();
            //quickPay.put("bankCardNo", rsaEncrypt(jjBankCardNo));
            //quickPay.put("amount", 10);

            //代扣
            //JSONObject daikouPay = new JSONObject();
            //daikouPay.put("bankCardNo", rsaEncrypt(jjBankCardNo));
            //daikouPay.put("amount", 10);

            //网关
            JSONObject gatewayPay = new JSONObject();
            gatewayPay.put("bankCode", "vbank");  //虚拟银行，专门用于测试环境
            gatewayPay.put("payType", rechargeDTO.getPayMethod().getGATEWAY().getPayType());
            gatewayPay.put("amount",rechargeDTO.getPayMethod().getGATEWAY().getAmount());

            //组装支付方式
            JSONObject payMethod = new JSONObject();
            payMethod.put("GATEWAY", gatewayPay);
            //payMethod.put("QUICKPAY", quickPay);
			//payMethod.put("DAIKOU", daikouPay);

            JSONObject param = new JSONObject();
            param.put("bizOrderNo", rechargeDTO.getBizOrderNo());
            param.put("bizUserId", rechargeDTO.getBizUserId());
            param.put("accountSetNo", rechargeDTO.getAccountSetNo());
            param.put("amount", rechargeDTO.getAmount());
            param.put("fee", rechargeDTO.getFee());
            param.put("validateType",rechargeDTO.getValidateType());
            param.put("frontUrl", rechargeDTO.getFrontUrl());
            param.put("backUrl", rechargeDTO.getBackUrl());
			param.put("ordErexpireDatetime", ordErexpireDatetime);
            param.put("payMethod", payMethod);
            param.put("goodsName",rechargeDTO.getGoodsName());
            param.put("industryCode", rechargeDTO.getIndustryCode());
            param.put("industryName", rechargeDTO.getIndustryName());
            param.put("source", rechargeDTO.getSource());
            param.put("summary", rechargeDTO.getSummary());
            param.put("extendInfo", rechargeDTO.getExtendInfo());

            log.info("depositApply request = {}",param);
            JSONObject response = client.request(soaName, OrderMethodEnum.DEPOSIT_APPLY.getValue(), param);
            log.info("depositApply response = {}" , response);

            return response;
        }catch(Exception e){
            e.printStackTrace();
            log.error("depositApply error",e);
        }
        return null;
    }
}
